package com.longrise.android.byjk.advertisement.InterstitialAD;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.longrise.android.byjk.R;
import com.longrise.android.byjk.advertisement.detail.ADdetailActivity;
import com.longrise.android.byjk.utils.Tracker_Google;

/**
 * Created by callxkj on 2016/12/19.
 * 弹出dialog式的插屏广告
 */

public class InterstitialDialog extends Dialog implements View.OnClickListener {


    private static final String TAG = "InterstitialDialog";
    private final Context mContext;
    private View mView;
    private ImageView mIv;
    private String mHrefurl;
    private int flage = 0;

    public InterstitialDialog(Context context) {
        this(context, R.style.AD_interstitial_dialog);
    }

    public InterstitialDialog(Context context, int themeResId) {
        super(context, themeResId);
        this.mContext = context;
        initView();
        initEvent();
    }

    private void initView() {
        mView = View.inflate(mContext, R.layout.ad_interstitial_dialog, null);
        mIv = (ImageView) mView.findViewById(R.id.ad_interstitial_dialog_iv);
        setContentView(mView);
    }

    private void initEvent() {
        mIv.setOnClickListener(this);
        this.setCanceledOnTouchOutside(false);
        //屏蔽返回键
        this.setOnKeyListener(new OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialogInterface, int keyCode, KeyEvent keyEvent) {
                if (keyCode == KeyEvent.KEYCODE_BACK) {
                    return true;
                }
                return false;
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ad_interstitial_dialog_iv: //点击广告图片 用浏览器显示
                Intent intent = new Intent(mContext, ADdetailActivity.class);
                intent.putExtra("url",mHrefurl);
                mContext.startActivity(intent);
                this.dismiss();
                Tracker_Google.getInstance().addEvent("Ads-home", "Click-ads", "进入广告");
                break;
        }
    }

    public void setImage(Bitmap bitmap, String hrefurl) {
        this.mHrefurl = hrefurl;

        WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
        float windowWidth = wm.getDefaultDisplay().getWidth();
        float bitmapWidth = bitmap.getWidth();
        float bitmapHeight = bitmap.getHeight();
        float limitWidth = (float) (windowWidth * 0.8);

        LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) mIv.getLayoutParams();
        if (bitmapWidth > limitWidth) {  //做一个宽度适配
            layoutParams.width = (int) limitWidth;
            layoutParams.height = (int) (bitmapHeight * (limitWidth / bitmapWidth));
        }else{
            layoutParams.width = (int)bitmapWidth;
            layoutParams.height = (int)bitmapHeight;
        }
        mIv.setLayoutParams(layoutParams);
        mIv.setImageBitmap(bitmap);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (MotionEvent.ACTION_DOWN == event.getAction()) { //外部点击两下然后弹窗消失
            flage++;
            if (flage >= 2) {
                this.dismiss();
                Tracker_Google.getInstance().addEvent("Ads-home", "Click-grey2", "第2次点击灰色区域");
            }else{
                Tracker_Google.getInstance().addEvent("Ads-home", "Click-grey1", "第1次点击灰色区域");
            }
        }
        return super.onTouchEvent(event);
    }
}
